混合、透明度
基本概念:
Alpha通道:颜色的第四个分量称为Alpha通道,指示颜色的 不透明度 。
源像素(Src):当前正在渲染的像素。
目标像素(Dest):已经在帧缓冲中的像素。
混合过程:
- 源像素和目标像素分别乘以源因子和目标因子。(因子调用
glBlendFunc()
指定) - 使用指定的blendEquation在混合生成新的颜色。(混合方程调用
glBlendEquation()
指定)
注意事项:
- 一般先渲染不透明物体再渲染透明物体。
- 渲染透明物体时也要按照从远到近顺序绘制。也可用顺序无关透明渲染技术(OIT)。
- 对于有体积的透明物体,可以用双面渲染,先渲染背面再渲染前面。(简单地禁用背面剔除有时会因绘制顺序而出现走样)
混合选项
glBlendFunc(…, …)参数:
GL_ZERO | ( *0, *0, *0, *0) |
GL_ONE | ( *1, *1, *1, *1) |
GL_SRC_COLOR | ( *R_src, *G_src, *B_src, *A_src) |
GL_ONE_MINUS_SRC_COLOR | ( *(1-R_src), *(1-G_src), *(1 -B_src), *(1-A_src)) |
GL_DST_COLOR | ( *R_dst, *G_dst, *B_dst, *A_dst) |
GL_ONE_MINUS_DST_COLOR | ( *(1-R_src), *(1-G_src), *(1 -B_src), *(1-A_src)) |
GL_SRC_ALPHA | ( *A_src, *A_src, *A_src, *A_src) |
GL_ONE_MINUS_SRC_ALPHA | ( *(1-A_src), *(1-A_src), *(1-A_src), *(1-A_src)) |
GL_DST_ALPHA | ( *A_dst, *A_dst, *A_dst, *A_dst) |
GL_ONE_MINUS_DST_ALPHA | ( *(1-A_dst), *(1-A_dst), *(1-A_dst), *(1-A_dst)) |
GL_CONSTANT_COLOR | ( *R_color, *G_color, *B_color, *A_color) |
… | … |
glBlendEquation(…)参数:
| | | | - | - | |GL_FUNC_ADD| result = src + dest| |GL_FUNC_SUBTRACT| result = src - dest| |GL_FUNC_REVERSE_SUBTRACT| result = dest - src| |GL_MIN| result = min(src, dest)| |GL_MAX| result = max(src, dest)|
常见混合方式
正常透明度混合:
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);
glBlendEquation(GL_FUNC_ADD);
正片叠底:
glBlendFunc(GL_DST_COLOR, GL_ZERO);